import { Message } from 'element-ui'
import axios from 'cloud-module-base/packages/httpConfig/http-plugin';
const Axios = axios.create()
const appPath = {
    'maneuver-scenario': '/maneuver-scenario',
    'cloud-system-manage': '/cloud-system-manage'
};
const allow_paths = ['ou/emp/list-all', 'ou/org/list', '/ou/emp/listEmp']
Axios.interceptors.response.use(
    async (response) => {
        if (response.config.url.includes('downloadFile')) return response.data
        if (!response)return Promise.reject('错误')
        if(response.data.status === 1) {
            if (response.config.method !== 'get' && allow_paths.every(url => !response.config.url.includes(url))) {
                Message.success(response.data.message)
            }
            console.log('response.data.data',response.data.data);
            return response.data.data
        } else {
            Message.error(response.data.message)
            return Promise.reject('错误')
        }
    },
    async (error) => {
        return Promise.reject(error)
    }
)
export default {
    'maneuver-scenario': {
        post: (url, data) => {
            return Axios({
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                method: 'post',
                url: '/gateway' + appPath['maneuver-scenario']  + '/api' + url,
                data 
            })
        },
        postForm: (url, formData) => {
            return Axios({
                headers: {
                    "Content-Type":"multipart/form-data;charset=utf-8"
                },
                transformRequest: [function(){
                    return formData;
                }],
                contentType: false,
                method: 'post',
                url: '/gateway' + appPath['maneuver-scenario']  + '/api' + url,
                data: formData
            })
        },
        getFile: (url, params) => {
            return Axios({
                responseType: 'blob',
                method: 'get',
                url: '/gateway' + appPath['maneuver-scenario']  + '/api' + url,
                params
            })
        },
        postJson: (url, data) => {
            return Axios({
                headers: {
                    'Content-Type': 'application/json; charset=UTF-8'
                },
                method: 'post',
                url: '/gateway' + appPath['maneuver-scenario']  + '/api' + url,
                data: JSON.stringify(data)
            })
        },
        get: (url, params) => {
            return Axios({
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                method: 'get',
                url: '/gateway' + appPath['maneuver-scenario']  + '/api' + url,
                params
            })
        }
    },
    'cloud-system-manage': {
        post: (url, data) => {
            return Axios({
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                method: 'post',
                url: '/gateway' + appPath['cloud-system-manage']  + '/api' + url,
                data 
            })
        },
        postForm: (url, formData) => {
            return Axios({
                headers: {
                    "Content-Type":"multipart/form-data;charset=utf-8"
                },
                transformRequest: [function(){
                    return formData;
                }],
                contentType: false,
                method: 'post',
                url: '/gateway' + appPath['cloud-system-manage']  + '/api' + url,
                data: formData
            })
        },
        
        postJson: (url, data) => {
            return Axios({
                headers: {
                    'Content-Type': 'application/json; charset=UTF-8'
                },
                method: 'post',
                url: '/gateway' + appPath['cloud-system-manage']  + '/api' + url,
                data: JSON.stringify(data)
            })
        },
        get: (url, params) => {
            return Axios({
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                method: 'get',
                url: '/gateway' + appPath['cloud-system-manage']  + '/api' + url,
                params
            })
        }
    },
    // default: {
    //     post: (url, params) => {
    //         return Axios.post('/gateway' + url, params).then(res => res);
    //     },
    //     get: (url, params) => {
    //         return Axios.get('/gateway' + url, {
    //             params: params
    //         }).then(res => res);
    //     },
    //     put: (url, params) => {
    //         return Axios.put('/gateway' + url, params).then(res => res);
    //     },
    //     delete : (url, params) => {
    //         return Axios.delete('/gateway' + url, {
    //             params: param
    //         }).then(res => res);
    //     }
    // }
}